<template>
  <div class="app-login">
    <img class="bg" src="@/assets/login/bg.png" alt="" />
    <div class="login">
      <div class="head">
        <img src="@/assets/logo.png" alt="" />
        <div class="title">优雅草星云智控AI物联网设备管理系统</div>
        <div class="version">v1.0.1版本</div>
      </div>
      <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item prop="username">
          <div class="login-input">
            <img src="@/assets/login/user.png" alt="" />
            <input type="text" placeholder="请输入用户名称" v-model="ruleForm.username" />
          </div>
        </el-form-item>
        <el-form-item prop="password">
          <div class="login-input">
            <img src="@/assets/login/password.png" alt="" />
            <input type="password" placeholder="请输入密码" v-model="ruleForm.password" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleFormRef)">
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="footer">
        <div class="footer-info">
          <a href="http://www.bjtimechina.com.cn/2025-05/18/content_43117417.html" target="_blank"
            class="footer-link">星云智控系统2025年5月15日发布v1.0.1新闻报道 </a><br>
            <a href="https://www.youyacao.com" target="_blank" class="footer-link">优雅草科技官网</a><br>
            <a href="https://xingyun.youyacao.com" target="_blank" class="footer-link">  成都星云智控科技有限公司</a>
        </div>
        <div class="footer-links">
          <a href="https://doc.apipost.net/docs/429f44f094e0000" target="_blank" class="footer-link">API接口地址</a>
        
          <a href="https://doc2.youyacao.com/web/#/131/2566" target="_blank" class="footer-link">  使用手册</a>

          <a href="https://doc2.youyacao.com/web/#/131/2559" target="_blank" class="footer-link">  安装教程</a>
          
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
import { reactive, ref } from "vue";
import { ElMessage } from "element-plus";
import { useStore } from "vuex";
import { authLogin } from "@/api/user";
const store = useStore();
const router = useRouter();
const ruleFormRef = ref();
const ruleForm = reactive({
  username: "",
  password: "",
});

const rules = reactive({
  username: [{ required: true, message: "请输入用户名称", trigger: "blur" }],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: "blur",
    },
  ],
});

const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      //登录
      const result = await authLogin(ruleForm);
      ElMessage.success("登录成功");
      localStorage.setItem("token", result.data.Token);
      localStorage.setItem("user_info", JSON.stringify(result.data));
      store.commit("changeUserinfo", result.data);
      setTimeout(() => {
        router.push({ path: "/index" });
      }, 1000);
    } else {
      console.log("error submit!", fields);
    }
  });
};
</script>
<style lang="scss" scoped>
::v-deep .el-form-item__content {
  margin-left: 0 !important;
}

.app-login {
  background: linear-gradient(44deg, #10244b 0%, #2656b1 100%);
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  width: 100%;

  .bg {
    width: 698.75px;
    height: 568.74px;
    margin-right: 157px;
  }

  .login {
    width: 488px;
    background: #ffffff;
    border-radius: 8px 8px 8px 8px;
    padding: 46px;
    box-sizing: border-box;
    position: relative;

    .head {
      text-align: center;
      margin-bottom: 25px;

      img {
        width: 66.91px;
      }

      .title {
        font-weight: bold;
        font-size: 20px;
        color: #091429;
        line-height: 23px;
        margin-top: 26px;
      }

      .version {
        font-size: 14px;
        color: #666;
        margin-top: 8px;
      }
    }

    .login-input {
      display: flex;
      align-items: center;
      width: 100%;
      padding: 0 12px;
      height: 54px;
      background: #f6f6f6;
      border-radius: 4px 4px 4px 4px;

      img {
        width: 24px;
        height: 24px;
        margin-right: 10px;
      }

      input {
        width: 100%;
        height: 100%;
        background: transparent;
        outline: none;
        border: none;
      }
    }

    .el-button {
      width: 100%;
      height: 54px;
      background: #008df0;
      border-radius: 4px 4px 4px 4px;
      box-sizing: border-box;
      font-weight: 500;
      font-size: 18px;
      color: #ffffff;
      margin-top: 44px;
    }

    .footer {
      text-align: center;
      padding: 0 46px;

      .footer-info {
        font-size: 12px;
        color: #666;
        line-height: 1.6;
        margin-bottom: 15px;
      }

      .footer-links {
        display: flex;
        justify-content: center;
        gap: 10px;
      }

      .footer-link {
        font-size: 12px;
        color: #008df0;
        text-decoration: none;

        &:hover {
          text-decoration: underline;
        }
      }
    }
  }
}
</style>